<?php

declare(strict_types=1);

namespace App\Amqp\Consumer;

use App\Service\Admin\TaskService;
use Hyperf\Amqp\Result;
use Hyperf\Amqp\Annotation\Consumer;
use Hyperf\Amqp\Message\ConsumerMessage;
use Hyperf\Di\Annotation\Inject;
use PhpAmqpLib\Message\AMQPMessage;

/**
 * @Consumer(exchange="taskEnd", routingKey="taskEnd", queue="taskEnd", name ="TaskEndConsumer", nums=1)
 */
class TaskEndConsumer extends ConsumerMessage
{

    /**
     * @Inject()
     * @var TaskService
     */
    protected $taskService;

    public function consumeMessage($data, AMQPMessage $message): string
    {
        $res = $this->taskService->endTask($data['task_id']);
        if (!$res) {
            return Result::REQUEUE;
        }
        return Result::ACK;
    }
}
